package com.example.frn.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.frn.dto.CategoryInsertDto;
import com.example.frn.dto.CategoryUpdateDTO;
import com.example.frn.dto.PageDTO;
import com.example.frn.entity.Category;
import com.example.frn.service.CategoryService;
import com.example.frn.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/category")

public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping("save")
// 关键：添加@RequestBody，接收前端JSON请求体
    public R save(@RequestBody CategoryInsertDto dto) {
        Category category = BeanUtil.copyProperties(dto, Category.class);
        if (categoryService.save(category)) {
            return R.ok(true);
        } else {
            return R.error(false);
        }
    }

    @PostMapping("remove/{cid}")
    public R remove(@PathVariable("cid") Integer cid) {
        if (categoryService.removeById(cid))
            return R.ok(true);
        else
            return R.error(false);
    }
    @PostMapping("update")
    public R update(@RequestBody CategoryUpdateDTO dto){
        Category category = BeanUtil.copyProperties(dto,Category.class);
        if(categoryService.updateById(category)){
            return R.ok(true);
        }
        else return R.error(false);
    }
    @GetMapping("list")
    public R list(){
        return R.ok(categoryService.list());
    }
    @GetMapping("listForFilm/{id}")
    public R listForFile(@PathVariable("id") Integer id){
        return R.ok(categoryService.listForFilm(id));
    }

    @GetMapping("detail/{cid}")
    public R detail(@PathVariable("cid") Integer cid){
        Category category = categoryService.getById(cid);
        return R.ok(category);
    }

    @PostMapping("page")
    public R page(@RequestBody PageDTO dto){
        return R.ok(categoryService.page(dto.getPageNum(),dto.getPageSize()));
    }

}
